/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gra.OBJloader;

import java.io.*;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

/**
 *
 * @author Grog
 */
public class Model_loader {

    public Model_loader() {
    }
   
    public Model Model_load(File f) throws FileNotFoundException, IOException {     
        BufferedReader reader = new BufferedReader(new FileReader(f));
        Model m = new Model();
        String line;
        Texture currentTexture = null;
        
        while((line = reader.readLine()) != null){
            
            if(line.startsWith("v ")){
            float x = Float.valueOf(line.split(" ")[1]);
            float y = Float.valueOf(line.split(" ")[2]);
            float z = Float.valueOf(line.split(" ")[3]);
            m.vertices.add(new Vector3f(x, y, z));
            } else if (line.startsWith("vn ")) {
            float x = Float.valueOf(line.split(" ")[1]);
            float y = Float.valueOf(line.split(" ")[2]);
            float z = Float.valueOf(line.split(" ")[3]);
            m.normals.add(new Vector3f(x, y, z));
            }else if(line.startsWith("vt "))
        {
            float x = Float.valueOf(line.split(" ")[1]);
            float y = Float.valueOf(line.split(" ")[2]);          
            m.texVert.add(new Vector2f(x, y));
        }else if(line.startsWith("f ")){
                Vector3f vertexIndictes = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),
                                                       Float.valueOf(line.split(" ")[2].split("/")[0]),
                                                       Float.valueOf(line.split(" ")[3].split("/")[0]) );
                
                Vector3f textureIndicies = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[1]), 
                                                        Float.valueOf(line.split(" ")[2].split("/")[1]),
                                                        Float.valueOf(line.split(" ")[3].split("/")[1]) );
                
                Vector3f normalIndictes = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),
                                                       Float.valueOf(line.split(" ")[2].split("/")[2]),
                                                       Float.valueOf(line.split(" ")[3].split("/")[2]) );
             
                m.faces.add(new Face(vertexIndictes,textureIndicies ,normalIndictes, currentTexture.getTextureID()));
            }else if(line.startsWith("g "))
        {
            if(line.length()>2)
            {
                String name = line.split(" ")[1];
                currentTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("model/" + name + ".png"));
                
                
                System.out.println(currentTexture.getTextureID());
            }
        } else if (currentTexture==null){
            currentTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("model/" + "tex" + ".png"));
             System.out.println("tex: "+currentTexture.getTextureID());
             
        }      
        }
        System.out.println(m.vertices.size() + " verticies");
    System.out.println(m.normals.size() + " normals");
    System.out.println(m.texVert.size() + " texture coordinates");
    System.out.println(m.faces.size() + " faces");
        
        reader.close();
      return m;
    }
}